<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/fea/stringMisc.php'; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/acc_conn.php'; ?>

<?php 
    function dorpc_gen_users($data, &$res) {
        $fNameArr = file($_SERVER['DOCUMENT_ROOT'] . "/accHestia/src/svr/init/test_v/mt/fname.txt");
        $lNameArr = file($_SERVER['DOCUMENT_ROOT'] . "/accHestia/src/svr/init/test_v/mt/name.txt");
        $fNameCnt = count($fNameArr);
        $lNameCnt = count($lNameArr);
        srand(time());
        
        $genCnt = $data["genCount"];//100; //要生成随机姓名的个数
        $arr = array();
        $ret = '<table style="width:80%"><tr><th>loginId</th> <th>姓名</th> <th>角色</th> </tr>';
        $w_str = "";
        for($i = 0; $i < $genCnt; ++$i) {
            $rnd_f = rand(0, $fNameCnt - 1);
            $rnd_l1 = rand(0, $lNameCnt - 1);

            $f_str = rtrim($fNameArr[$rnd_f]);
            $l1_str = rtrim($lNameArr[$rnd_l1]);

            $rnd3 = rand(0, 100);
            $uName = "";
            if($rnd3 < 40) {  //40%几率产生 一个字的名字
                $uName = $f_str . $l1_str;    
            } else { //60几率
                if(2 == mb_strlen($f_str, 'utf-8')) { //姓为双字
                    if(rand(0, 100) < 10) { //双字姓中, 10%的几率 两字名
                        $rnd_l2 = rand(0, $lNameCnt - 1);
                        $l2_str = rtrim($lNameArr[$rnd_l2]);
                        $uName = $f_str . $l1_str . $l2_str; 
                    } else {  //双字姓中, 90%的几率 单字名
                        $uName = $f_str . $l1_str; 
                    }
                } else { //姓为单字
                    if(rand(0, 100) < 40) { //单字姓中, 40%单字名
                        $uName = $f_str . $l1_str; 
                    } else {
                        $rnd_l2 = rand(0, $lNameCnt - 1);
                        $l2_str = rtrim($lNameArr[$rnd_l2]);
                        $uName = $f_str . $l1_str . $l2_str; 
                    }
                }
            }
            $loginId = getAlphas($uName);
            if("" == $loginId) {
                continue;
            }
            if(in_array($loginId, $arr)) {
                $loginId = ($loginId . $i);
            }
            array_push($arr, $loginId);
            
            $rnd_role = rand(0, 100);
            $role_id = 0;
            $role_str = "";
            if($rnd_role < 30) {
                $role_id = 10004; //会计员
                $role_str = "会计员";
            } else if($rnd_role < 50) {
                $role_id = 10005; //出纳员
                $role_str = "出纳员";
            } else if($rnd_role < 90) {
                $role_id = 10006; //业务员
                $role_str = "业务员";
            } else {
                $role_id = 10100; //其它人员
                $role_str = "其它人员";
            }
            $w_str .= "insert into users(id, loginId, pwd, uName, role_id, email) values('" .
                ($i + 101) . "', '" . $loginId . "', '81dc9bdb52d04dc20036dbd8313ed055', '" .
                $uName . "', " . $role_id . ", '" . $loginId . "@789.com');\r\n";
            if($i % 2) {
                $ret .= "<tr><td>" . $loginId . "</td> <td>" . $uName . "</td> <td>" . $role_str . "</td></tr>\r\n";    
            } else {
                $ret .= "<tr class='acc_ood_row'><td>" . $loginId . "</td> <td>" . $uName . "</td> <td>" . $role_str . "</td></tr>\r\n";
            }
        }
        $ret .= "</table>";
        
        $fpath = $_SERVER['DOCUMENT_ROOT'] . "/accHestia/db/test/userDummy.sql";
        $f = fopen($fpath, "w");
        if(!$f) {
            set_res_NG($res, "failed on creating file: " . $fpath);
            return;
        }
        fwrite($f, $w_str);
        fclose($f);

        $res["hData"] = $ret;
        set_res_OK($res);
    }
    
    function dorpc_add_gened_users($data, &$res) {
        $fpath = $_SERVER['DOCUMENT_ROOT'] . "/accHestia/db/test/userDummy.sql";
        $sqlArr = file($fpath);
        $sql_cnt = count($sqlArr);
        
        
        $db = new acc_DB();
        for($i = 0; $i < $sql_cnt; $i++) {
            $ssql = rtrim($sqlArr[$i]);
            if (!$db->query($ssql)) {
                set_res_NG($res, $ssql);
                return;
            }
        }
        $res["ok_count"] = $sql_cnt;
        set_res_OK($res);
    }
?>